import javax.swing.JOptionPane;

/**
 * MethodsProgram.java
 * 
 * Program that implements problems 7.33 and 7.34 from the textbook Java Program
 * Design 5.0
 * 
 * 
 * @author Andrew Shore
 * @course CSCE 145 Section 004
 * @version 1.0
 */

public class MethodsProgram {

	/**
	 * Constants
	 */
	public static final String DONE = "done";
	public static final String MENU = "Enter \"pig latin\" to convert a phrase" +
				"\nEnter \"distance to light years\" to convert a distance"+
				"\n Enter \"done\" to quit the program";
	
	//other constants
	public static final String DISTANCE="distance to light years";
	public static final String PIG="pig latin";
	public static final int SPEED_OF_LIGHT=299792500;
	public static final double METERS_PER_KILOMETER=.001;
	public static final int SECONDS_IN_A_YEAR=31536000;
	
	
	
	/**
	 * postcondition: the program has displayed a greeting
	 *    in a JOptionPane for this program
	 */
	public static void displayGreeting(){
    	JOptionPane.showMessageDialog(null, "Welcome to the Methods Program. "+
    			"\nThis program converts distance in kilometers to distance in light years or "+
    				"\n translates English to Pig Latin");
    	
    }
	
	/**
	 * postcondition: the user has been asked to enter their choice,
	 * 	   based on the MENU options. That value is returned.
	 */
    public static String showMenu(){
    	String answer=JOptionPane.showInputDialog(null, MENU);
    
    	return answer;
    }

      

      public static void handleDistanceInLightYears(){
    	  String answer=JOptionPane.showInputDialog(null, "Please enter a distance in kilometers to be converted:");
    	  double distance =Double.parseDouble(answer);						//changes answer to type double
    	  double lightYears=MethodsProgram.ConvertLightYears(distance);		//calls method asked for in the problem
    	  JOptionPane.showMessageDialog(null, distance+" kilometers equals "+ lightYears+" light years.");
      }
     
    
    
    
   public static double ConvertLightYears(double distanceInKilometers){
	   //converts kilometers to light years using formula
	   double lightYears=distanceInKilometers*(1/((SPEED_OF_LIGHT)*(METERS_PER_KILOMETER)*SECONDS_IN_A_YEAR));
	   return lightYears;
   }
    
    
    
    
    
    
    
      public static void handlePigLatin(){
    	  String answer=JOptionPane.showInputDialog(null, "Please enter a word to be translated to Pig Latin:");
    	  String word=convertToPigLatin(answer);			//calls on method below
    	  JOptionPane.showMessageDialog(null, answer+" translated to Pig Latin is "+word);
      }
 
    public static String convertToPigLatin(String english){
    	String firstLetter=english.substring(0,1);
    	int wordLength=english.length();
    	String word="";
    	
    	//if first letter of the word is a vowel "ay" is added to the end of the word
    		if(firstLetter.equalsIgnoreCase("a") || firstLetter.equalsIgnoreCase("e")||firstLetter.equalsIgnoreCase("i")
    				||firstLetter.equalsIgnoreCase("o")||firstLetter.equalsIgnoreCase("u")){
    			 word=english.substring(1,wordLength)+"ay";
    		}else{
    			word=english.substring(1,wordLength)+firstLetter+"ay";
    			
    		}
    	
    	return word;
    }
    
    //displays ending message and exits program
    public static void handleDone(){
    	JOptionPane.showMessageDialog(null, "Thank you for using the Methods Program. Goodbye.");
    	System.exit(0);
    }
    //if user enters an invalid input the program tells them
    public static void handleError(){
    	JOptionPane.showMessageDialog(null, "Invalid input, please enter 'pig latin' or 'distance to light years'");
    }
    
    
  
    
    
    
    /**
     * This method contains the main algorithm and should be
     * 	 	complete. 
     */
    public static void main(String[] args) {
    	//1. declare variables
    	String userInput = "";
    	
    	//2. display greeting
    	MethodsProgram.displayGreeting();
    	
    	do{
  
    		//3. show menu and get user input
    		userInput = MethodsProgram.showMenu();
    		
    		//4. user enters distance in light years
    		if(userInput.equalsIgnoreCase(DISTANCE)){
    			//4a. handle distance in light years
    			MethodsProgram.handleDistanceInLightYears();
    		}
    		//5. user enters pig latin
    		else if(userInput.equalsIgnoreCase(PIG)){
    			//5a. handle pig latin
    			MethodsProgram.handlePigLatin();
    		}
    		//6. user enters done
    		else if(userInput.equalsIgnoreCase(DONE)){
    			//6a handle done
    			MethodsProgram.handleDone();
    		}
    		//7. user enters invalid input
    		else{
    			//7a. handle error
    			MethodsProgram.handleError();
    		}

    	}while(!userInput.equalsIgnoreCase(DONE));
	


    } // end main method

} //end MethodsProgram class

